package com.heavenbird.book.dao.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.heavenbird.book.BookConst;
import com.heavenbird.book.dao.interfaces.IUserBillDAO;
import com.heavenbird.book.entity.UserBill;
import com.heavenbird.common.util.DBUtil;

public class UserBillDAOImpl implements IUserBillDAO {
	
	private static transient Log log = LogFactory.getLog(UserBillDAOImpl.class);
	
	public UserBill[] queryRelBillByAcctId(long userId, long acctId, int sts) throws Exception {
		Session session = DBUtil.getSession();
		Criteria criteria = session.createCriteria(UserBill.class);
		criteria.add(Restrictions.eq("userId", userId));
		criteria.add(Restrictions.or(Restrictions.eq("srcAcctId", acctId), Restrictions.eq("descAcctId", acctId)));
		criteria.add(Restrictions.eq("sts", sts));
		criteria.addOrder(Order.asc("createDate"));
		List list = criteria.list();
		return (UserBill[]) list.toArray(new UserBill[list.size()]);
	}
	
	public UserBill[] queryBillByCategoryId(long userId, long categoryId, int sts) throws Exception {
		Session session = DBUtil.getSession();
		Criteria criteria = session.createCriteria(UserBill.class);
		criteria.add(Restrictions.eq("userId", userId));
		criteria.add(Restrictions.eq("category", categoryId));
		criteria.add(Restrictions.eq("sts", sts));
		criteria.addOrder(Order.asc("createDate"));
		List list = criteria.list();
		return (UserBill[]) list.toArray(new UserBill[list.size()]);
	}
	
	public UserBill[] queryRelBillByAcctIdWithoutAdjust(long userId, long acctId, int sts) throws Exception {
		Session session = DBUtil.getSession();
		Criteria criteria = session.createCriteria(UserBill.class);
		criteria.add(Restrictions.eq("userId", userId));
		criteria.add(Restrictions.or(Restrictions.eq("srcAcctId", acctId), Restrictions.eq("descAcctId", acctId)));
		criteria.add(Restrictions.not(Restrictions.in("category", new Long[]{BookConst.USERCATEGORYDEF.CATEGORY_ID_ADJUST_INIT, BookConst.USERCATEGORYDEF.CATEGORY_ID_ADJUST_ADJUST})));
		criteria.add(Restrictions.eq("sts", sts));
		criteria.addOrder(Order.asc("createDate"));
		List list = criteria.list();
		return (UserBill[]) list.toArray(new UserBill[list.size()]);
	}
	
	public UserBill[] queryRelBillByAcctIdWithAdjust(long userId, long acctId, int sts) throws Exception {
		Session session = DBUtil.getSession();
		Criteria criteria = session.createCriteria(UserBill.class);
		criteria.add(Restrictions.eq("userId", userId));
		criteria.add(Restrictions.or(Restrictions.eq("srcAcctId", acctId), Restrictions.eq("descAcctId", acctId)));
		criteria.add(Restrictions.in("category", new Long[]{BookConst.USERCATEGORYDEF.CATEGORY_ID_ADJUST_INIT, BookConst.USERCATEGORYDEF.CATEGORY_ID_ADJUST_ADJUST}));
		criteria.add(Restrictions.eq("sts", sts));
		criteria.addOrder(Order.asc("createDate"));
		List list = criteria.list();
		return (UserBill[]) list.toArray(new UserBill[list.size()]);
	}

	public void updateUserBill(UserBill userBill) throws Exception {
		Session session = DBUtil.getSession();
		session.update(userBill);
	}
	
	public void newUserBill(UserBill userBill) throws Exception {
		Session session = DBUtil.getSession();
		session.save(userBill);
	}
}
